package manager.fileupload.controller;
import io.swagger.annotations.Api;
import manager.fileupload.service.FileUploadService;
import manager.fileupload.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = {"/admin/file"})
@Api(value = "商品模块")
public class FileUploadController {
    private final ResourceLoader resourceLoader;
    @Autowired
    public FileUploadController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
    @Autowired
    private FileUploadService fileUploadService;
    @Value("${web.upload-path}")
    private String path;
    @Value("${web.domain}")
    private String domain;
    @RequestMapping(value = "upload",method = RequestMethod.POST)
    public  Map<String,String> upload(MultipartFile file,String index, String orgUrl){
        Map<String,String> resultMap=new HashMap<String,String>();
        String url = fileUploadService.uploadImage(file);
        resultMap.put("index",index);
        resultMap.put("url",url);
        return resultMap;
    }
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public  void delete(@RequestBody Map<String,String> map){
          FileUtils.deleteDescFile(path,map.get("deleteUrl"));
    }

}
